package MP
{
    import flash.utils.ByteArray;

    public class MapdataTranfrom 
    {

        public static var mapdata:MapVO;

        public function tranfromToMapVO(bytes:ByteArray):MapVO
        {
            return (this.convertMapVO(bytes));
        }
        private function convertMapVO(bytes:ByteArray):MapVO
        {
            var mapvo:MapVO;
            var obj:Object;
            var node:Node;
            var i:int;
            var buffer:int;
            var travel:Boolean;
            var ride:Boolean;
            var ispk:Boolean;
            var safe:Boolean;
            var isMask:Boolean;
            var isReflex:Boolean;
            var isWabao:Boolean;
            var k:int;
            var obj2:Object;
            var num:int;
            if (mapdata == null)
            {
                mapdata = new MapVO();
            } else
            {
                mapdata.dispose();
            };
            mapvo = mapdata;
            var str:String = bytes.readMultiByte(bytes.length, "utf-8");
            obj = JSON.parse(str);
            mapvo.version = obj.version;
            mapvo.mapId = obj.id;
            mapvo.mapWidth = obj.width;
            mapvo.mapHeight = obj.height;
            mapvo.nodeWidth = obj.gridWidth;
            mapvo.nodeHeight = obj.gridHeight;
            mapvo.tileWidth = obj.tileWidth;
            mapvo.tileHeight = obj.tileHeight;
            mapvo.col = obj.col;
            mapvo.row = obj.row;
            mapvo.name = obj.name;
            var list:Array = obj.gridList;
            MapConfig.MAP_NODE_WIDTH = mapvo.nodeWidth;
            MapConfig.MAP_NODE_HEIGHT = mapvo.nodeHeight;
            var nodeArray:Array = [];
            var j:int;
            while (j < mapvo.row)
            {
                i = 0;
                while (i < mapvo.col)
                {
                    if (nodeArray[j] == null)
                    {
                        nodeArray[j] = [];
                    };
                    buffer = list[((j * mapvo.col) + i)];
                    travel = Boolean((buffer & 0x01));
                    ride = Boolean((buffer & (1 << 1)));
                    ispk = Boolean((buffer & (1 << 2)));
                    safe = Boolean((buffer & (1 << 3)));
                    isMask = Boolean((buffer & (1 << 4)));
                    isReflex = Boolean((buffer & (1 << 5)));
                    isWabao = Boolean((buffer & (1 << 6)));
                    node = Node.getNode();
                    node.x = i;
                    node.y = j;
                    node.ride = ride;
                    node.isJump = isReflex;
                    node.isWabao = isWabao;
                    node.ispk = ispk;
                    node.isSafe = safe;
                    node.isMask = isMask;
                    node.walkAble = travel;
                    nodeArray[j][i] = node;
                    i++;
                };
                j++;
            };
            mapvo.nodeArray = nodeArray;
            var special:Array = (obj.special as Array);
            if (special)
            {
                k = 0;
                while (k < special.length)
                {
                    obj2 = special[k];
                    node = mapvo.getNode(obj2.y, obj2.x);
                    node.isTaFang = Boolean((obj2.type & 0x01));
                    node.isXunLongJue = Boolean((obj2.type & 0x04));
                    node.isWeiXian = Boolean((obj2.type & 0x08));
                    if (node.isXunLongJue == false)
                    {
                        node.isXunLongJue = Boolean((obj2.type & 0x10));
                    };
                    if (node.isWeiXian)
                    {
                        mapvo.isHasWeixianArea = true;
                    };
                    k++;
                };
            };
            var tileCol:uint = Math.ceil((mapvo.mapWidth / MapConfig.MAP_CLIP_IMAGE_WIDTH));
            var tileRow:uint = Math.ceil((mapvo.mapHeight / MapConfig.MAP_CLIP_IMAGE_HEIGHT));
            mapvo.tileArray = [];
            i = 0;
            while (i < tileRow)
            {
                if (mapvo.tileArray[i] == null)
                {
                    mapvo.tileArray[i] = [];
                };
                j = 0;
                while (j < tileCol)
                {
                    num = (((tileCol * i) + j) + 1);
                    mapvo.tileArray[i][j] = num;
                    j++;
                };
                i++;
            };
            return (mapvo);
        }
        private function readShort(source:ByteArray):int
        {
            var buffer:ByteArray = new ByteArray();
            var v1:int = source.readByte();
            var v2:int = source.readByte();
            buffer.writeByte(v2);
            buffer.writeByte(v1);
            buffer.position = 0;
            return (buffer.readShort());
        }

    }
}//package yr_engine.scene.map.libarys
